package com.fingard.dsp.bank.directbank.zbf01;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IQRPay;
import com.fingard.dsp.bank.reqrespobj.qrpay.ReqGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.qrpay.RetAtsGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqUnifiedOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsUnifiedOrder;

import java.util.HashMap;
import java.util.Map;

/**
 * @author XueJy on 2019/1/28.
 * @version 1.0
 */
public class ZBF01QRPay extends ZBF01UnifiedOrder implements IQRPay {
    @Override
    public RetAtsGenQRInfo genQRInfo(ReqGenQRInfo p_req) {
        return null;
    }

    @Override
    public RetAtsUnifiedOrder submitQROrder(ReqUnifiedOrder p_req) {
        if ("ZBFNative03".equals(p_req.reqHeader.commandCode) || "ZBFNative04".equals(p_req.reqHeader.commandCode)) {
            RetAtsUnifiedOrder retAts = new RetAtsUnifiedOrder();
            retAts.rdSeq = p_req.rdSeq;

            try {
                ActSetItem actItem = this.getBankConfig().getActSetItem(p_req.ownAct, "unifiedorder");
                if (actItem != null) {
                    String alignCode = p_req.alignCode;
                    String merno = p_req.merchantId;
                    String terid = p_req.terid;

                    StringBuilder codeurl = new StringBuilder();
                    WriteBankLogLn("下单地址(qryLeiLongUrl):" + bankFront.getUrl("qryLeiLongUrl"));
                    codeurl.append(bankFront.getUrl("qryLeiLongUrl") + "?");
                    String payInfo = "02";
                    if ("1".equals(p_req.realNameFlag) && "1".equals(p_req.realNameType)) {
                        payInfo = "01";
                    }
                    codeurl.append("alignCode=" + alignCode);
                    codeurl.append("&policyId=" + p_req.rdSeq);
                    codeurl.append("&payInfo=" + payInfo);
                    codeurl.append("&merno=" + merno);
                    codeurl.append("&terid=" + terid);
                    codeurl.append("&bizNo=" + p_req.rdSeq);
                    String methodCode = "22";
                    if ("ZBFNative04".equals(p_req.reqHeader.commandCode)) {
                        methodCode = "12";
                    }
                    codeurl.append("&methodCode=" + methodCode);

                    Map<String, Object> signString = new HashMap<String, Object>();
                    signString.put("alignCode", alignCode);
                    signString.put("merno", merno);
                    signString.put("methodCode", methodCode);
                    signString.put("payInfo", payInfo);
                    signString.put("policyId", p_req.rdSeq);
                    signString.put("terid", terid);
                    signString.put("bizNo", p_req.rdSeq);
                    String stringA = buildObjectSortWaitSign(signString, false);
                    WriteBankLogLn("参与签名的参数串:" + stringA);
                    WriteBankLogLn("雷龙科技中保付安全码:" + actItem.saltKey);
                    String macValue = hmacsha256(stringA, actItem.saltKey);
                    WriteBankLogLn("签名结果:" + macValue);
                    codeurl.append("&mac=" + macValue);
                    WriteBankLogLn("生成的二维码链接:\r\n" + codeurl.toString());
                    retAts.respCode = "0";
                    retAts.qrInfo = codeurl.toString();
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息!";
                    retAts.qrInfo = "";
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                retAts.respCode = "-1";
                retAts.respInfo = ex.getMessage();
                WriteBankLogLn(ex);
            }

            return retAts;
        } else {
            return submitUnifiedOrder(p_req);
        }
    }

    @Override
    public RetAtsQueryOrder queryQRPay(ReqQueryOrder p_req) {
        return queryUnifiedOrder(p_req);
    }
}